import Vue from 'vue'
import axios from 'axios'
import store from '../store'
export var wechat = null;
export var loginU3D = null;
import {
    errFun,
} from "../tools.js"
export const DEV_BASE_URL = 'http://192.168.1.101:8080'; // 开发环境下的服务器，会不定时重启
export const PROD_BASE_URL = 'http://api.shapejoy.com'; // 线上环境服务器
export const DEV_STABLE_BASE_URL = 'http://192.168.1.101:8082/shapejoy'; // 开发环境下服务器，一般不会重启
export const LOGIN_URL = 'http://192.168.1.101:2333'; //登录设备的地址
export const TEST = 'http://test.shapejoy.com/'; // 测试服务器

// 登陆API的设置
export const DEV_BASE_LOGIN_URL = "http://192.168.1.101:2333"
export const PROD_BASELOGIN_URL = "https://login.shapejoy.com"
    //切换接口
if (process.env.NODE_ENV !== 'production') 
{
    // 线下
    wechat = axios.create({
        baseURL: TEST
    });
    wechat.interceptors.request.use(function(config) {
        //显示loading
        store.dispatch('CHANGE_LOADING_SHOW');
        // console.log("aaaa")
        return config; //返回的config代表对请求做出处理
    });
    wechat.interceptors.response.use(function(response) {
        //隐藏loading
        setTimeout(function() {
            store.dispatch('HIDDEN_LOADING_SHOW');
        }, 50)
        return response;
    }, errFun);
    // wechat.interceptors.response.eject(inter);
    loginU3D = axios.create({
        baseURL: LOGIN_URL
    })
} 
// 线上
else 
{
    wechat = axios.create({
        baseURL: PROD_BASE_URL
    });
    wechat.interceptors.request.use(function(config) {
        //显示loading
        store.dispatch('CHANGE_LOADING_SHOW');
        return config; //返回的config代表对请求做出处理
    });
    wechat.interceptors.response.use(function(response) {
        //隐藏loading
        setTimeout(function() {
            store.dispatch('HIDDEN_LOADING_SHOW');
        }, 50)
        return response;

    }, errFun);
    loginU3D = axios.create({
        baseURL: PROD_BASELOGIN_URL
    })

}
//七牛的api
export const QINIU_API = "http://up.qiniu.com";
wechat.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
loginU3D.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
// set token
export function setAPIToken(token) {
    wechat.defaults.headers.common['x-token'] = token;
    loginU3D.defaults.headers.common['x-token'] = token;
    localStorage.setItem('token', token);
    //console.log('api get' + localStorage.getItem('token'))
}
export default {
    //获取个人体重
    getEdit: function(data) {
        return wechat.get("/wechat/api/v3/user");
    },
    //获取用户体重
    getWeight: function() {
        return wechat.get("/wechat/api/v3/user/data");
    },
    //用户信息
    getUserInfo() {
        return wechat.get("/wechat/api/v3/me")
    },
    //优惠券
    getUserCoupons: function() {
        return wechat.get('/wechat/api/v3/coupon')
    },
    //全部课程
    getallCourse: function(date) {
        return wechat.get('/wechat/api/v3/course/all/' + date)
    },
    //获得筛选课程
    getCourse: function(uri) {
        return wechat.get('/wechat/api/v3/course/by' + uri)
    },
    //全部计划
    getAllPlan: function() {
        return wechat.get('/wechat/api/v3/plan?type=all')
    },
    //获取课程介绍
    getCourseDetail: function(id) {
        return wechat.get('/wechat/api/v3/course/' + id)
    },
    //获取计划介绍
    getPlanDetail: function(id) {
        return wechat.get('/wechat/api/v3/plan/detail/' + id)
    },
    //获取当前计划
    getCurrentPlan: function(date) {
        return wechat({
            method: 'get',
            url: '/wechat/api/v3/plan/',
            params: {
                date: date,
            }
        })
    },
    //添加计划
    plusPlan: function(id, date) {
        return wechat.get('/wechat/api/v3/plan/choose/' + id, {
            params: {
                date: date
            }
        })
    },
    //添加课程
    plusCourse: function(id, date) {
        // return wechat.post('/wechat/api/v3/course/addOption')
        return wechat({
            method: 'POST',
            url: '/wechat/api/v3/course/addOption',
            data: {
                id: id,
                date: date
            }
        })
    },
    //删除当前计划
    delete_current_plan: function(id) {
        return wechat({
            method: 'delete',
            url: '/wechat/api/v3/plan/delete',
            params: {
                id: id
            }
        })
    },
    //精选课程
    getFeaturedCourse: function() {
        return wechat.get('/wechat/api/v3/course/fine')
    },
    // 获取所有的周报
    getAllWeekly() {
        return wechat.get('/wechat/api/v3/weekly/page?page=1');
    },
    // 周报
    getWeekly: function() {
        return wechat.get('/wechat/api/v3/weekly')
    },
    getWeeklyByid(id) {
        return wechat.get('/wechat/api/v3/weekly/getByWeeklyId?id=' + id);
    },
    // 自选课程
    getSelfCourse: function() {
        return wechat.get('/wechat/api/v3/fit/optioncourse')
    },
    //填写性别
    setUserInfo: function(gender) {
        return wechat({
            method: 'post',
            url: "/wechat/api/v3/user",
            data: {
                sex: gender,
            }
        })
    },
    //填写体重
    setUserInfoH: function(age, height, weight) {
        return wechat({
            method: 'post',
            url: "/wechat/api/v3/user",
            data: {
                age: age,
                height: height,
                weight: weight
            }
        })
    },
    //修改的api----xscf
    //今日界面的状态及数据
    getTodayStatus(date) {
        return wechat({
            method: 'get',
            url: '/wechat/api/v3/fit/status',
            params: {
                date: date,
            }
        })
    },
    //今日界面的删除课程
    getDeleteCourse(pushid) {
        return wechat({
            method: 'delete',
            url: '/wechat/api/v3/fit/optioncourse',
            params: {
                pushid: pushid,
            }
        })
    },
    //今日界面的计划课程
    getPlaneCourse(date) {
        return wechat({
            url: '/wechat/api/v3/fit/plancourse',
            method: 'get',
            params: {
                date: date,
            }
        })
    },
    //今日界面课程表的课程数据-----自选课程
    getCourseInfo(date) {
        return wechat({
            url: '/wechat/api/v3/fit/getOptioncourse',
            method: 'get',
            params: {
                date: date,
            }
        })
    },
    //打卡界面的心率
    getHeart(data) {
        return wechat({
            url: '/wechat/api/v3/user/data',
            method: 'post',
            data: {
                data: data,
            }
        })
    },
    //打卡界面的上传图片获取图片token
    getImgToken() {
        return wechat({
            url: '/wechat/api/v3/upload/token?type=image',
            method: 'get',
        })
    },
    //打卡界面的上传图片完成返回后台的值
    sendSuccess(data) {
        return wechat({
            url: '/wechat/api/v3/upload/imageCallback',
            method: 'post',
            data: {
                data: data,
            }
        })
    },
    //打卡界面渲染已经上传三餐图片的api
    getUploadImg(date) {
        return wechat({
            url: '/wechat/api/v3/diet',
            params: {
                date: date,
            }
        })
    },
    //打卡界面日历上传三餐图片的日期
    getUploadFood(date) {
        return wechat({
            url: '/wechat/api/v3/calendar/month/' + date,
        })
    },
    //获取明细的数据
    getMingXinfo(page) {
        return wechat({
            url: '/wechat/api/v3/bill/list',
            method: 'get',
            params: {
                page: page
            }
        })
    },
    //个人资料界面的
    setEditInfo(data) {
        return wechat({
            url: '/wechat/api/v3/user',
            method: 'post',
            data: data,
        })
    },
    //健身记录的数据
    getJianshenInfo(page) {
        return wechat({
            url: '/wechat/api/v3/fit/history/',
            method: 'get',
            params: {
                page: page
            }
        })
    },
    //我的收藏的视频数据
    getCollectVideo(date) {
        return wechat({
            url: '/wechat/api/v3/fit/video/get',
            method: 'get',
            params: {
                date: date
            }
        })
    },
    //我的收藏日期
    getCollectDate(date) {
        return wechat({
            url: '/wechat/api/v3/fit/video/date',
            params: {
                year: date.year,
                month: date.month,
            }
        })
    },
    //登录设备
    loginU(id, enterpriseID) {
        return loginU3D({
            url: '/wechat/' + id + '/untiy-device-au',
            method: 'post',
            data: {
                id: id,
                enterpriseId: enterpriseID
            }
        })
    },
    //结果页的数据
    getResult(id) {
        return wechat({
            url: '/wechat/api/v3/fit/history/detail?id=' + id,
            method: 'get',
        })
    },
    //课程跳转至结果页
    getResultBY(id) {
        return wechat({
            url: '/wechat/api/v3/fit/history/detailBy?pushid=' + id,
            method: 'get',
        })
    },
    //遥控器
    getOk(id) {
        return wechat({
            method: 'POST',
            url: '/wechat/api/v3/rc/command',
            data: {
                id: id
            }
        })
    },
    //修改圈圈
    getDateCurriculum(year,month){
	    return wechat({
	            url: '/wechat/api/v3/calendar/year?year=' + year + '&month=' + month,
	            method: 'get',
	        })
        },
    //关注的用户隐藏绿色心，不关注显示
    getCloseRctips() {
        return wechat({
            url: '/wechat/api/v3/unity/close_rctips',
            method: 'get',
        })
    },

}